var isRunning = false
var maxNum = 0
var interV = ""
var formData = ""
var selectFormData = ""
var mainKey = ""

$(document).ready(function() {
    // $('a').click(function() {
    //     var link = $(this).parents("tr");
    //     link.remove();
    // });
    $('tr').click(function() {
        $(this).remove();
    });
});

function del_form_item(e) {
    if (!isRunning) {
        var item = new Array()
        var td = $(e).parents("tr").children("td")
        var len = td.length - 2
        for (var i = 0; i < len; i++) {
            item.push(td[i].textContent)
        }
    
        var index = -1
    
        for (var i = 0; i < selectFormData.length; i++) {
            if (selectFormData[i].toString() == item.toString()) {
                index = i;
                break;
            }
        }
    
        if (index > -1) {
            selectFormData.splice(index, 1)
        }
        console.log(index, selectFormData);
        
        $('#form_table').html(draw_form(selectFormData))
    }
}

function loadLucky1(){
    var luckbtn = document.getElementsByClassName("btn")[0];//监听抽奖按钮
    luckbtn.addEventListener("click", function(e){
        loadForm();
        var str=Math.floor(Math.random()*(formData.length+1)).toString();
        var roll=document.getElementsByClassName("roll")[0];
        roll.style.cssText = "animation: circle1 4s ease-out";
        roll.style.transition = "all " + str * 0.15 + "s ease-out";
        roll.style.transitionDelay = "4s";
        roll.style.marginTop = -98 * str + "px";
    });
    var resetbtn = document.getElementsByClassName("btn")[1];
    resetbtn.addEventListener("click", function(e) {
        var first = document.getElementsByClassName("roll")[0];
        first.style.cssText = "";
        first.style.transform = "";
    });
}

function loadForm() {
    var box = $('#rollBox')
    for (var i = 1; i < FINAL_FORM.length; i++) {
        box.append('<li>' + FINAL_FORM[i].join(' ') + '</li>')
    }
    for (var i = 1; i < FINAL_FORM.length; i++) {
        box.append('<li>' + FINAL_FORM[i].join(' ') + '</li>')
    }
    addKeyFrames(FINAL_FORM.length*(-98))
    console.log(box)
}

function addKeyFrames(y){
    var style = document.createElement('style');
    style.type = 'text/css';
    var keyFrames = '\
    @keyframes circle1 {\
        0% {\
            transform: translateY(0px);\
        }\
        100% {\
            transform: translateY(A_DYNAMIC_VALUEpx);\
        }\
    }';
    style.innerHTML = keyFrames.replace(/A_DYNAMIC_VALUE/g, y);
    document.getElementsByTagName('head')[0].appendChild(style);
}
function draw_form(form) {
    var html = ""
    var curLine = ""
    selectFormData = new Array(form.length)
    // head
    selectFormData[0] = new Array()
    mainKey.forEach(index => {
        curLine += "<th>" + form[0][index] + "</th>"
        selectFormData[0].push(form[0][index])
    });
    html += "<tr>" + curLine + "</tr>"
    
    for (var i = 1; i < form.length; i++) {
        selectFormData[i] = new Array()
        v = form[i]
        curLine = ""
        mainKey.forEach(index => {
            curLine += "<td>" + v[index] + "</td>"
            selectFormData[i].push(v[index])
        });
        curLine += "<td><a href=\"#\">修改</a></td><td><a onclick=\"del_form_item(this)\">删除</a></td>"
        html += "<tr>" + curLine + "</tr>"
    }

    return html
}


function start_lottery() {
    if (!isRunning) {
        if (selectFormData == "") {
            alert("请上传名单")
            return
        }
        isRunning = !isRunning;
        max_num = selectFormData.length;
        interV = setInterval(function(){
            $("#show_lottery").html("中奖:"+selectFormData[Math.floor(Math.random()*(max_num - 1) + 1)])
        }, 200);

    } else {
        alert("lottery is running")
    }
}

function stop_lottery() {
    if (isRunning) {
        clearInterval(interV)
        isRunning = !isRunning
        $("#show_lottery").html("<h1>" + $("#show_lottery").html() + "</h1>")
    }

}

function test_but(headers) {
    var nav = $('#nav')
    nav.addClass('nav-fixed')
    innerHtml = ""
    headers.forEach(e => {
        innerHtml += '<input type="checkbox" name="' + e + '" id="' + e + '">' + e
    })
    // innerHtml += '<input type="submit" value="选择主码" id="choose_code" onclick="select_head()">'
    innerHtml += '<input type="button" value="选择主码" id="choose_code">'
    $('#headers_form').html(innerHtml)
    $(nav).show()
    $('#choose_code').click(function(){
        console.log($(this));
        mainKey = new Array()
        var len = $(this).siblings().length
        for (var i = 0; i < len; i++) {
            if ($(this).siblings()[i].checked == true) {
                mainKey.push(i)
            }
        }
        if (mainKey.length > 0) {
            show_form()
            $('#nav').hide()
        } else {
            alert("请选择主码")
        }
    })
    // return $('.choose_code')
}

function show_form() {
    var html = draw_form(formData)
    $('#form_table').html(html)
    $('#form_table').show()
    // formData.shift()
}

function uploadFile() {
    var file = $('#upload_file')[0].files[0]
    Papa.parse(file, {
        complete: function(results) {
            formData = results.data
            test_but(formData[0])
        }
    })

}

function select_head(headers) {
    
}